package mercede.allManagerBeans;

import javax.ejb.Stateless;
import mercede.allManagers.ExcursionMnager;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import mercede.allTable.Excursion;
import mercede.allDTOs.ExcursionDTO;


import java.util.ArrayList;
import java.util.List;


/**
 * Session Bean implementation class ExcursionMnagerBean
 */
@Stateless
public class ExcursionMnagerBean implements ExcursionMnager {

	
	 @PersistenceContext
	   private EntityManager em;
		
		@Resource
		private EJBContext context;
		

	    @Override
	    public void save(ExcursionDTO Excursiondto) {
	            Excursion newExcursion= new Excursion(Excursiondto);
	            em.persist(newExcursion);
	    }
	    
	       
	    @Override
	    public void remove(int hid) {
	 		Excursion Excursion = find(hid);
	         em.remove(Excursion);
	 	}
	     
	     public void remove(Excursion Excursion) {
	     	em.remove(Excursion);
	 	}
	     
	   
	   private Excursion find(int hid) {
	   return em.find(Excursion.class, hid);
	   }
	    
	   @Override
	   public ArrayList<?> getExcursion() {
	   return (ArrayList<?>) em.createNamedQuery("Excursion.findAll", Excursion.class).getResultList();
	   }
	   
	   
	   @Override
	   public ExcursionDTO getExcursionDTO(int hid) {
		   Excursion newExcursion =find(hid);
		   ExcursionDTO Excursiondto = convertToDTO(newExcursion);
	       return Excursiondto;
	   }
	   

	   @Override
	   public List<ExcursionDTO> getListExcursionDTO() {
	       List<Excursion> newExcursion= em.createNamedQuery("Excursion.findAll", Excursion.class).getResultList();
	       List<ExcursionDTO> Excursiondto = new ArrayList<>();
	       for(Excursion h: newExcursion) {
	    	   Excursiondto.add(convertToDTO(h));
	       }
	       return Excursiondto;
	   }
	   
	   

	   @Override
	   public List<ExcursionDTO> getListExcursionDTOByDest(String ecity) {
		   
	      Query query=em.createNamedQuery("Excursion.FIND_BY_DEST", Excursion.class);
	      query.setParameter("ecity",ecity);
		   List<Excursion> newHotle= query.getResultList();
	       List<ExcursionDTO> Excursiondto = new ArrayList<>();
	       for(Excursion h: newHotle) {
	    	   Excursiondto.add(convertToDTO(h));
	       }
	       return Excursiondto;
	   }
	   
	   public static List<ExcursionDTO> convertToDTOList(List<Excursion> ExcursionList) {
	       List<ExcursionDTO> ExcursiondtoList = new ArrayList<ExcursionDTO>();
	       for(Excursion h: ExcursionList){
	    	   ExcursiondtoList.add(convertToDTO(h));
	       }
	       return  ExcursiondtoList;
	}   
	   
	   
	    public String getPrincipalName() {
	        return context.getCallerPrincipal().getName();
	    }
	    
	    public static  ExcursionDTO convertToDTO(Excursion Excursion)
	    {
	    	
	    	ExcursionDTO newExcursiondto=new ExcursionDTO();

	    	newExcursiondto.seteID(Excursion.getEID());
	    	newExcursiondto.seteName(Excursion.getEName());
	    	newExcursiondto.seteCity(Excursion.getECity());
	    	newExcursiondto.seteDescription(Excursion.getEDescription());
	    	newExcursiondto.setePrice(Excursion.getEPrice());

	    	
	    	return newExcursiondto;
	    	
	    }



}
